package com.bkgy.controller.tbCgl1870SteelgradeSpe2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.tbCgl1870SteelgradeSpe2.entity.TbCgl1870SteelgradeSpe2;
import com.bkgy.controller.tbCgl1870SteelgradeSpe2.entity.pdo.TbCgl1870SteelgradeSpe2Res;
import com.bkgy.controller.tbCgl1870SteelgradeSpe2.mapper.ITbCgl1870SteelgradeSpe2Mapper;
import com.bkgy.controller.tbCgl1870SteelgradeSpe2.service.ITbCgl1870SteelgradeSpe2Service;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbCgl1870SteelgradeSpe2ServiceImpl extends ServiceImpl<ITbCgl1870SteelgradeSpe2Mapper, TbCgl1870SteelgradeSpe2> implements ITbCgl1870SteelgradeSpe2Service {

    @Override
    public ResultData<String> saveTbCgl1870SteelgradeSpe2(TbCgl1870SteelgradeSpe2 obj) {
        boolean res = false;
        try {
            res = save(obj);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResultData.ResultDataSuccess(res ? "SUCCESS" : "FAIL");
    }

    @Override
    public ResultData<String> updateTbCgl1870SteelgradeSpe2(TbCgl1870SteelgradeSpe2 obj) {
        boolean res = updateById(obj);
        return ResultData.ResultDataSuccess(res ? "SUCCESS" : "FAIL");
    }

    @Override
    public ResultData<String> deleteTbCgl1870SteelgradeSpe2(TbCgl1870SteelgradeSpe2 obj) {
        boolean res = removeById(obj);
        return ResultData.ResultDataSuccess(res ? "SUCCESS" : "FAIL");
    }

    @Override
    public ResultData selectPage(TbCgl1870SteelgradeSpe2 req) {
        LambdaQueryWrapper<TbCgl1870SteelgradeSpe2> wrapper = new LambdaQueryWrapper<>();
        if (req.getContorIstate() != null) {
            wrapper.eq(TbCgl1870SteelgradeSpe2::getContorIstate, req.getContorIstate());
        }
        List<TbCgl1870SteelgradeSpe2> list = list(wrapper);
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "P8控，温度无限制");
        map.put(2, "P8控，±20度");
        map.put(3, "P9控，A类");
        map.put(4, "p9控，B类");

        List<TbCgl1870SteelgradeSpe2Res> listRes = new ArrayList<>();
        for (TbCgl1870SteelgradeSpe2 obj : list) {
            TbCgl1870SteelgradeSpe2Res res = new TbCgl1870SteelgradeSpe2Res();
            BeanUtils.copyProperties(obj, res);
            res.setContorIstate(map.get(obj.getContorIstate()));
            res.setCal(obj.getCal() != null && obj.getCal() == 1 ? "未投用（测试钢种）" : "");
            listRes.add(res);
        }
        return ResultData.ResultDataSuccess(listRes, listRes.size());
    }

}
